import { defineConfig } from 'vite';
import { svelte } from '@sveltejs/vite-plugin-svelte';
import path from 'path';

// https://vitejs.dev/config/
export default defineConfig(({ command, mode }) => {
  const common = {
    resolve: {
      alias: [{ find: '@', replacement: path.resolve(__dirname, 'src') }]
    }
  };

  if (command === 'serve') {
    return {
      server: {
        open: false,
        host: 'dev.10jqka.com.cn',
        port: 8080,
        proxy: {
          '/vtuber': {
            target: 'http://avatar.aicubes.cn/',
            changeOrigin: true,
            ws: false
          }
        }
      },
      plugins: [svelte({})],
      ...common
    };
  }

  if (command === 'build') {
    return {
      ...common,
      plugins: [svelte({})],
      build: {
        outDir: 'pack',
        cssTarget: 'chrome49',
        cssCodeSplit: true,
        lib: {
          entry: path.resolve(__dirname, 'src/lib/index.ts'),
          name: 'svelteSdk',
          fileName: (format) => `vtuber.${format}.js`
        },
        rollupOptions: {}
      }
    };
  }
});
